package diploma.kisel.modelas.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.SpringLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import net.sourceforge.jdatepicker.JDateComponentFactory;
import net.sourceforge.jdatepicker.JDatePicker;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;

import org.joda.time.DateTime;

import diploma.kisel.modelas.domain.Project;
import diploma.kisel.modelas.domain.User;
import diploma.kisel.modelas.gui.common.CurrentVar;
import diploma.kisel.modelas.service.ProjectServiceImpl;
import diploma.kisel.modelas.service.api.ProjectService;
import diploma.kisel.modelas.validation.Validation;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * It is used to create project and saved it into database.
 * 
 * @author Nikolay Kisel
 *
 */
public class ProjectWindow extends JFrame {

	private final int nameLength = 45;
	private final int shortNameLength = 8;
	private final int versionLength = 10;
	private final int studNameLength = 45;
	private final int studSurnameLength = 45;
	private final int studGroupLength = 45;

	private JPanel contentPane;
	private JTextField textFieldName;
	private JTextField textFieldShortName;
	private JTextField textFieldVersion;
	private JTextField textFieldDate;
	private JTextField textFieldStName;
	private JTextField textFieldStSurname;
	private JTextField textFieldStGroup;

	private static JFrame frame;

//	private static Project currProject;

	private ProjectService projectService = new ProjectServiceImpl();

	
	/**
	 * Create the frame.
	 */
	public ProjectWindow() {
		setTitle("Создание проекта");
//		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 537, 248);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);

		JLabel label = new JLabel("Название проекта:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label, 10,
				SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, label, 10,
				SpringLayout.WEST, contentPane);
		contentPane.add(label);

		textFieldName = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldName, -3,
				SpringLayout.NORTH, label);
		sl_contentPane.putConstraint(SpringLayout.WEST, textFieldName, 40,
				SpringLayout.EAST, label);
		contentPane.add(textFieldName);
		textFieldName.setColumns(10);

		JLabel label_1 = new JLabel("Сокращенное название:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_1, 21,
				SpringLayout.SOUTH, label);
		sl_contentPane.putConstraint(SpringLayout.WEST, label_1, 0,
				SpringLayout.WEST, label);
		contentPane.add(label_1);

		textFieldShortName = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldShortName,
				-3, SpringLayout.NORTH, label_1);
		sl_contentPane.putConstraint(SpringLayout.WEST, textFieldShortName, 0,
				SpringLayout.WEST, textFieldName);
		contentPane.add(textFieldShortName);
		textFieldShortName.setColumns(10);

		JLabel label_2 = new JLabel("Версия:");
		sl_contentPane.putConstraint(SpringLayout.WEST, label_2, 10,
				SpringLayout.WEST, contentPane);
		contentPane.add(label_2);

		textFieldVersion = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldVersion, 16,
				SpringLayout.SOUTH, textFieldShortName);
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_2, 3,
				SpringLayout.NORTH, textFieldVersion);
		sl_contentPane.putConstraint(SpringLayout.WEST, textFieldVersion, 0,
				SpringLayout.WEST, textFieldName);
		contentPane.add(textFieldVersion);
		textFieldVersion.setColumns(10);

		JLabel label_3 = new JLabel("Дата:");
		sl_contentPane.putConstraint(SpringLayout.WEST, label_3, 0,
				SpringLayout.WEST, label);
		contentPane.add(label_3);

//		JDatePicker dp = JDateComponentFactory.createJDatePicker();
//		dp.setTextEditable(true);
//		dp.setShowYearButtons(true);
		
		textFieldDate = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldDate, 17,
				SpringLayout.SOUTH, textFieldVersion);
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_3, 3,
				SpringLayout.NORTH, textFieldDate);
		sl_contentPane.putConstraint(SpringLayout.WEST, textFieldDate, 0,
				SpringLayout.WEST, textFieldName);
		contentPane.add(textFieldDate);
		textFieldDate.setColumns(10);

		JLabel label_4 = new JLabel("Имя:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_4, 0,
				SpringLayout.NORTH, label);
		sl_contentPane.putConstraint(SpringLayout.WEST, label_4, 30,
				SpringLayout.EAST, textFieldName);
		contentPane.add(label_4);

		textFieldStName = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldStName, -3,
				SpringLayout.NORTH, label);
		sl_contentPane.putConstraint(SpringLayout.EAST, textFieldStName, -24,
				SpringLayout.EAST, contentPane);
		contentPane.add(textFieldStName);
		textFieldStName.setColumns(10);

		JLabel label_5 = new JLabel("Фамилия:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_5, 0,
				SpringLayout.NORTH, label_1);
		sl_contentPane.putConstraint(SpringLayout.WEST, label_5, 0,
				SpringLayout.WEST, label_4);
		contentPane.add(label_5);

		textFieldStSurname = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldStSurname,
				-3, SpringLayout.NORTH, label_1);
		sl_contentPane.putConstraint(SpringLayout.WEST, textFieldStSurname, 0,
				SpringLayout.WEST, textFieldStName);
		contentPane.add(textFieldStSurname);
		textFieldStSurname.setColumns(10);

		JLabel label_6 = new JLabel("Группа:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_6, 0,
				SpringLayout.NORTH, label_2);
		sl_contentPane.putConstraint(SpringLayout.WEST, label_6, 0,
				SpringLayout.WEST, label_4);
		contentPane.add(label_6);

		textFieldStGroup = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldStGroup, -3,
				SpringLayout.NORTH, label_2);
		sl_contentPane.putConstraint(SpringLayout.WEST, textFieldStGroup, 0,
				SpringLayout.WEST, textFieldStName);
		contentPane.add(textFieldStGroup);
		textFieldStGroup.setColumns(10);

		JButton button = new JButton("Отменить");
		sl_contentPane.putConstraint(SpringLayout.EAST, button, 0, SpringLayout.EAST, textFieldStName);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setVisible(false);
			}
		});
		contentPane.add(button);

		JButton button_1 = new JButton("Сохранить");
		sl_contentPane.putConstraint(SpringLayout.SOUTH, button_1, -10, SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, button_1, -7, SpringLayout.WEST, button);
		sl_contentPane.putConstraint(SpringLayout.NORTH, button, 0, SpringLayout.NORTH, button_1);
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				saveProject();
			}
		});
		contentPane.add(button_1);

		User user = CurrentVar.getCurrentUser();
		textFieldStName.setText(user.getName());
		textFieldStSurname.setText(user.getSurname());
		textFieldStGroup.setText(user.getGroup());
	}

	public void saveProject() {
		Project project = new Project();
		String projectName = textFieldName.getText();
		if (Validation.isLengthBetween(projectName, 1, nameLength)) {
			project.setName(projectName);
		} else {
			JOptionPane.showMessageDialog(frame,
					"Неверное название проекта. Название может включать до"
							+ nameLength + " символов.");
			return;
		}
		String shortName = textFieldShortName.getText();
		if (Validation.isLengthBetween(shortName, 0, shortNameLength))
			project.setShortName(shortName);
		else {
			JOptionPane.showMessageDialog(frame,
					"Сокращенное название может быть до" + shortName
							+ " символов.");
			return;
		}
		String version = textFieldVersion.getText();
		if (Validation.isLengthBetween(version, 0, versionLength))
			project.setVersion(version);
		else {
			JOptionPane.showMessageDialog(frame,
					"Неправильное название версии. Версия может включить до "
							+ versionLength + " символов");
			return;
		}
		User user = CurrentVar.getCurrentUser();
		String studName = textFieldStName.getText();
		if (Validation.isLengthBetween(studName, 1, studNameLength))
			user.setName(studName);
		else {
			JOptionPane.showMessageDialog(frame,
					"Неправильное указано имя студента");
			return;
		}
		String studSurname = textFieldStSurname.getText();
		if (Validation.isLengthBetween(studSurname, 1, studSurnameLength))
			user.setSurname(studSurname);
		else {
			JOptionPane.showMessageDialog(frame,
					"Неправильно указана фамилия студента");
			return;
		}
		String studGroup = textFieldStGroup.getText();
		if (Validation.isLengthBetween(studGroup, 1, studGroupLength))
			user.setGroup(studGroup);
		else {
			JOptionPane.showMessageDialog(frame,
					"Неправильно указана группа студента");
			return;
		}
		String dateString = textFieldDate.getText();
		if (Validation.isDate(dateString)) {
			DateTime date = new DateTime(Validation.reformatDateString(dateString));
			project.setStartDate(date);
		} else {
			JOptionPane.showMessageDialog(frame, "Неправильный формат даты");
			return;
		}
		project.setUser(user);
		// DateTime date =
		projectService.save(project);
		CurrentVar.setCurrentProject(project);
//		System.out.println(currProject.getId());
		JOptionPane.showMessageDialog(frame, "Проект успешно сохранен.", "Сообщение", JOptionPane.INFORMATION_MESSAGE);
		frame.setVisible(false);
//		openNewConceptWindow();

	}
	
	public void openNewConceptWindow() {
		ConceptWindow conceptWindow = new ConceptWindow();
		ConceptWindow.setFrame(conceptWindow);
		conceptWindow.setVisible(true);
	}

	public static JFrame getFrame() {
		return frame;
	}

	public static void setFrame(JFrame frame) {
		ProjectWindow.frame = frame;
	}


}
